//! sky自创语言的token表示

use std::fmt::{Display, Formatter};

#[derive(Debug, Clone, PartialEq)]
pub struct Token {
    r#type: i32,
    literal: String,
    line_num: usize,
    begin: usize,
    end: usize,
}

impl Token {
    pub fn new(r#type: i32, literal: &str, line_num: usize, begin: usize, end: usize) -> Self {
        Self {
            r#type,
            literal: literal.to_string(),
            line_num,
            begin,
            end,
        }
    }

    #[inline]
    pub fn r#type(&self) -> i32 {
        self.r#type
    }

    #[inline]
    pub fn literal_ref(&self) -> &str {
        &self.literal
    }

    #[inline]
    pub fn line_num(&self) -> usize {
        self.line_num
    }

    #[inline]
    pub fn begin(&self) -> usize {
        self.begin
    }

    #[inline]
    pub fn set_begin(&mut self, begin: usize) {
        self.begin = begin;
    }

    #[inline]
    pub fn set_end(&mut self, end: usize) {
        self.end = end;
    }

    #[inline]
    pub fn set_line_num(&mut self, num: usize) {
        self.line_num = num;
    }

    #[inline]
    pub fn end(&self) -> usize {
        self.end
    }
}

impl Display for Token {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.write_fmt(format_args!(
            "({},{},{})",
            self.line_num(),
            self.r#type(),
            self.literal_ref()
        ))
    }
}
